//
//  DetailActivityViewController.m
//  豆瓣HW
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailActivityViewController.h"
#import "DetailActivityView.h"
#import "LoginViewController.h"
#import "Activity.h"
#import "DataBaseHandle.h"
#import "User.h"

@interface DetailActivityViewController ()

@end

@implementation DetailActivityViewController

- (void)dealloc
{
    [_act release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    self.navigationItem.title = self.act.title;
    
//    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(barAction:)];
    
    
    DetailActivityView *detailV = [[DetailActivityView alloc] initWithFrame:self.view.frame];
    detailV.labelTitle.text = self.act.title;
    NSRange range = {5, 11};
    NSString *time = [self.act.begin_time substringWithRange:range];
    NSString *time1 = [self.act.end_time substringWithRange:range];
    NSString *time2 = [time stringByAppendingString:@" -- "];
    NSString *time3 = [time2 stringByAppendingString:time1];
    detailV.labelTime.text = time3;
    NSString *owner_name = [self.act.owner objectForKey:@"name"];
    detailV.labelPerson.text = owner_name;
    detailV.labelStyleName.text = self.act.category_name;
    detailV.labelPosition.text = self.act.address;
    detailV.lableActivityIntroduceDate.text = self.act.content;
    detailV.imageV.image = self.act.finalImage;
    
    [self.view addSubview:detailV];
    [detailV release];
}

- (void)barAction:(UIBarButtonItem *)sender
{
    if (sender == self.navigationItem.leftBarButtonItem) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
        if (user.name == nil) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        } else {
            NSMutableArray *tempArr = [[DataBaseHandle shareDataBase] selectByActivityTitle:self.act.title];
            if (![tempArr containsObject:user.name]) {
                
                NSRange range = {5, 11};
                NSString *time = [self.act.begin_time substringWithRange:range];
                NSString *time1 = [self.act.end_time substringWithRange:range];
                NSString *time2 = [time stringByAppendingString:@" -- "];
                NSString *time3 = [time2 stringByAppendingString:time1];
                    NSString *owner_name = [self.act.owner objectForKey:@"name"];
//                [[DataBaseHandle shareDataBase] insertDataWithActivity:user ActivityName:self.act.title labelTitle:self.act.title labelTime:time3 labelPerson:owner_name labelStyleName:self.act.category_name labelPosition:self.act.address lableActivityIntroduceDate:self.act.content];
                
                user.title = self.act.title;
                user.labelTitle = self.act.title;
                user.labelTime = time3;
                user.labelPerson = owner_name;
                user.labelStyleName = self.act.category_name;
                user.labelPosition = self.act.address;
                user.lableActivityIntroduceDate = self.act.content;
                
                [[DataBaseHandle shareDataBase] insertDataWithActivity:user];
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
                [alertC dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
                
            } else {
//                NSLog(@"已收藏");
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已被收藏过" preferredStyle:UIAlertControllerStyleAlert];
                [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                    
                }]];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
                
            }
            
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
